﻿using System;
using RockCorn.Internals;

namespace RockCorn.Objects
{
    public abstract class ObjectWithLifetime : GameObject
    {
        private int lifetime;

        public int Lifetime
        {
            get { return this.lifetime; }
            private set
            {
                if (value > 0)
                    this.lifetime = value;
                else
                    throw new ArgumentOutOfRangeException("lifetime", "Lifetime should be a positive integer number (cycles).");
            }
        }

        protected ObjectWithLifetime(MatrixCoords topLeft, char[,] body, int lifetime)
            : base(topLeft, body)
        {
            this.Lifetime = lifetime;
        }

        public override void Update()
        {
            --this.lifetime;
            if (this.lifetime == 0) this.IsDestroyed = true;
        }
    }
}
